package com.cnap.multiclouldmgmt.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.cnap.multiclouldmgmt.model.CommonResult;
import com.cnap.multiclouldmgmt.model.RepByPage;
import com.cnap.multiclouldmgmt.model.enums.CloudVendorsEnum;
import com.cnap.multiclouldmgmt.model.gpu.MicroSvcGpuInfoModel;
import com.cnap.multiclouldmgmt.model.gpu.NodeQueryModel;
import com.cnap.multiclouldmgmt.model.images.*;
import com.cnap.multiclouldmgmt.model.microSvc.MsgInfo;
import com.cnap.multiclouldmgmt.model.vm.*;
import com.cnap.multiclouldmgmt.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Objects;

/**
 * 对接其他后端相关系统
 * @author dyq
 */
@Api(tags ="镜像系统")
@RequestMapping(path = "/cnap/multicloudmgmt/v1/image")
@RestController
@ResponseBody
public class ImageController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ImageController.class);

    @Autowired
    private ImageInfoService imageInfoService;

    @ApiOperation(value = "8-1 上传镜像文件",httpMethod = "POST",
            notes = "")
    @RequestMapping(path = "/uploadImageInfo",method = RequestMethod.POST)
    public CommonResult<String> uploadImageInfo(ImageInfoModel imageInfoModel,@RequestParam(
            "iconfile") MultipartFile multipartFile) {

        try {

            //返回上传id
            return CommonResult.success(imageInfoService.uploadImageInfo(imageInfoModel));
        }catch (Exception e){

            LOGGER.error("ImageController uploadImageInfo error",e);

            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 分片上传
     *
     * @param uploadPatchReq 上传参数
     * @param multipartFile  分片文件
     * @return 结果
     */
    @ApiOperation(value = "上传分片", notes = "上传分片", httpMethod = "POST")
    @PostMapping("/patchUpload")
    public CommonResult<PatchRes> uploadPatch(@Validated UploadPatchReq uploadPatchReq, @RequestParam(
            "patch") MultipartFile multipartFile) {
        LOGGER.info("[file]: upload patch file, {}", uploadPatchReq);
        PatchRes patchRes = imageInfoService.uploadPatch(uploadPatchReq, multipartFile);
        return CommonResult.success(patchRes);
    }


    /**
     * 合并文件
     *
     * @param mergeReq 合并参数
     * @return 结果
     */
    @PostMapping("/patchMerge")
    @ApiOperation(value = "合并文件", notes = "合并文件", httpMethod = "POST")
    public CommonResult<String> mergePatch(@Validated @RequestBody MergeReq mergeReq) {
        LOGGER.info("[file]: merge patch file, {}", mergeReq);
        imageInfoService.mergePatchFile(mergeReq);
        return CommonResult.success("success");
    }


    /**
     * 获取镜像信息
     *
     * @return 结果
     */
    @GetMapping("/getImageInfos")
    @ApiOperation(value = "获取镜像信息", notes = "合并文件", httpMethod = "POST")
    public CommonResult<RepByPage<ImageInfoModel>> getImageInfos(String imageName,String imageScope,int pageSize, int pageNo) {

        RepByPage<ImageInfoModel> result = new RepByPage<>();
        result.setResult(new ArrayList<>());
        result.setTotalNum(1);
        result.setPageSize(pageSize);
        result.setPageNo(pageNo);

        ImageInfoModel imageInfoModel = new ImageInfoModel();
        result.getResult().add(imageInfoModel);
        imageInfoModel.setUserName("admin");
        imageInfoModel.setDownloadTimes(20);
        imageInfoModel.setImageDescribe("ubuntu");
        imageInfoModel.setImageFilePath("file/image_file/ubuntu-18.04.6-live-server-amd64.iso");
        imageInfoModel.setImageIconFilePath("file/image_icon/testicon01.png");
        imageInfoModel.setImageName("ubuntu-18.04.6-live-server-amd64.iso");
        imageInfoModel.setImageScope("public");
        imageInfoModel.setImageScore(3);
        imageInfoModel.setOsName("linux");


        return CommonResult.success(result);
    }


    /**
     * 查询上传进度
     *
     * @param id 应用id
     * @return 结果
     */
    @GetMapping("/progress/{id}")
    @ApiOperation(value = "查询进度", notes = "查询进度", httpMethod = "GET")
    public CommonResult<ProgressVO> queryProgress(@PathVariable("id") String id) {

        return Objects.nonNull(null) ? CommonResult.success(null) : CommonResult.failed("未找到该应用上传进度");
    }

}
